<?php
error_reporting(0);  //代码增加  By www.17Aym.com
session_start();

//查看手册,到阿里大鱼官网用相关信息替换105 106 113行关键信息

header("Content-type:text/html; charset=UTF-8");


function random($length = 6 , $numeric = 0) {
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	if($numeric) {
		$hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
	} else {
		$hash = '';
		$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
		$max = strlen($chars) - 1;
		for($i = 0; $i < $length; $i++) {
			$hash .= $chars[mt_rand(0, $max)];
		}
	}
	return $hash;
}

function read_file($file_name) {
	$content = '';
	$filename = date('Ymd').'/'.$file_name.'.log';
	if(function_exists('file_get_contents')) {
		@$content = file_get_contents($filename);
	} else {
		if(@$fp = fopen($filename, 'r')) {
			@$content = fread($fp, filesize($filename));
			@fclose($fp);
		}
	}
	$content = explode("\r\n",$content);
	return end($content);
}

/* 代码修改_start  BY  www.ecshop68.com */
$mobile = isset($_POST['mobile']) ? trim($_POST['mobile']) : '';
$mobile_code = isset($_POST['mobile_code']) ?  trim($_POST['mobile_code']) : '';
/* 代码修改_end  BY  www.ecshop68.com */


if($_GET['act']=='check')
{
	if(time() - $_SESSION['time'] > 30*60 )
	{
		 unset($_SESSION['mobile_code']);
		 exit(json_encode(array('msg'=>'验证码超过30分钟。')));	
	}
	else
	{
		if($mobile!=$_SESSION['mobile'] or $mobile_code!=$_SESSION['mobile_code'])
		{
			exit(json_encode(array('msg'=>'手机验证码输入错误。')));	
		}
		else
		{
			exit(json_encode(array('code'=>'2')));	
		}
	}
 
}

if($_GET['act']=='send')
{
	
	if(empty($mobile)){
		exit(json_encode(array('msg'=>'手机号码不能为空')));
	}
	
	$preg='/^1[0-9]{10}$/';//简单的方法
	if(!preg_match($preg,$mobile)) {
		exit(json_encode(array('msg'=>'手机号码格式不正确')));
	 }	
	
	$mobile_code =random(6,1);
	
	
	if($_SESSION['mobile']){
		//exit(json_encode(array('msg'=> read_file($mobile) )));
		if(strtotime(read_file($mobile))>(time()-60)){
			exit(json_encode(array('msg'=>'获取验证码太过频繁，一分钟之内只能获取一次。')));	
		}
	}
	
	$num = sendSMS($mobile,$mobile_code);
	if($num == true)
	{
		$_SESSION['mobile']=$mobile;
		$_SESSION['mobile_code']=$mobile_code;
		$_SESSION['time'] = time();
		exit(json_encode(array('code'=>2)));	
	}
	else
	{
		exit(json_encode(array('msg'=>'手机验证码发送失败。')));	
	}
}

function sendSMS($phone,$code){
include "smsdk/TopSdk.php";
date_default_timezone_set('Asia/Shanghai'); 
$c = new TopClient;
$c->appkey = '23286569';//APPKEY
$c->secretKey = '298dfa463db195ab3248730f050701cb';//APPSECRETKEY
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req->setExtend("23286569");
$req->setSmsType("normal");
$req->setSmsFreeSignName("注册验证");
$req->setSmsParam("{\"code\":\"".$code."\",\"product\":\"易乐生鲜\",\"item\":\"易乐生鲜\"}");
$req->setRecNum($phone);
$req->setSmsTemplateCode("SMS_3145292");
$resp = $c->execute($req);
if($resp->result->success!='true'){
	return false;
}else{
	return true;
}
}

?>
